# -* coding= utf-8 -*-
from StrategyParameter import AdviseX, Advise, Common


class VolPrice:
    close = 0
    close_prev = 0

    running_time = None

    direction = AdviseX.Unknown
    volume_direction = AdviseX.Unknown

    running_direction = AdviseX.Unknown
    running_volume_direction = AdviseX.Unknown

    running_count = 0
    running_points = 0
    running_rise = 0

    running_volume_count = 0
    running_volume_points = 0
    running_volume_rise = 0

    rise = 0
    rise_prev = 0
    rise_points = 0

    vol = 0
    vol_prev = 0
    vol_running = 0
    vol_mean = 0
    vol_mean_prev = 0
    vol_times = 0

    up_shadow_line = 0
    down_shadow_line = 0

    cant_up = False
    cant_down = False

    def analyze(self, bar, market_data, begin_trade_time):
        market_data.index.names = ["datetime", "code"]
        data = market_data[-10:]
        self.close = data.close[-1]
        self.close_prev = data.close[-2]
        self.direction = Common.get_last_direction(bar, data.close)
        self.volume_direction = Common.get_last_direction(bar, data.volume)
        self.vol = data.volume[-1]
        self.vol_prev = data.volume[-2]
        self.vol_mean_prev = self.vol_mean
        self.vol_mean = round(data.volume.mean(), 4)
        self.vol_times = round(self.vol / self.vol_mean, 4)
        self.rise_points = self.close - self.close_prev
        self.rise = round(self.rise_points / self.close, 4)
        self.rise_prev = round((self.close_prev - data.close[-3]) / self.close_prev, 4)

        self.up_shadow_line = round((bar.high - bar.close) / (bar.high - bar.low), 4)
        self.down_shadow_line = round((bar.close - bar.low) / (bar.high - bar.low), 4)

        # 连续3次以上成交量收缩，价格不动，变向可能很大
        self.running_direction, self.running_count, self.running_points, self.running_rise = \
            self.get_running(bar, data.close, "收盘价")
        self.running_volume_direction, self.running_volume_count, self.running_volume_points, self.running_volume_rise = \
            self.get_running(bar, data.volume, "成交量")

        tips = f"成交量：{self.vol_prev} => {self.vol:.0f} {self.volume_direction} /{self.vol_mean:.0f} = {self.vol_times:.2f}倍 "
        if self.running_volume_count > 1:
            tips += f"成交量：连续{self.running_volume_count}次{self.running_volume_direction} 达{self.running_volume_points}({self.running_volume_rise * 100:.2f}%)"
        print(f"{bar.running_time} {tips}")

        tips = f"收盘价：{self.close_prev} => {self.close} {self.direction} {self.rise_points}({self.rise * 100:.2f}%) "
        if self.running_count > 1:
            tips += f"收盘价：连续{self.running_count}次{self.running_direction} 达{self.running_points}({self.running_rise * 100:.2f}%)"
        print(f"{bar.running_time} {tips}")

        # # 收盘价小幅下跌，同时成交量下跌，而且成交量不小于1.5，说明市场没有下跌能量
        # if 0 < abs(rise) < 0.0007 and vol_now < vol_prev and times > 1.5 and over_points > 0:
        #     advise.direction = self.turn_direction(direction)
        #     advise.degree = 10
        #     advise.cant_up = True if direction == AdviseX.Up else False
        #     advise.cant_down = True if direction == AdviseX.Down else False
        #     advise.reason = f"收盘价小幅变动，同时成交量下跌，而且成交量不小于1.5，说明市场没有上涨/下跌能量"
        #     return advise
        #
        # if 0 < abs(rise) < 0.0007 and vol_now > vol_prev and times < 0.8 and \
        #         direction != (AdviseX.Up if self.ma05_trend.degree > 0 else AdviseX.Down if self.ma05_trend.degree < 0 else AdviseX.Unknown):
        #     advise.direction = self.turn_direction(direction)
        #     advise.degree = 10
        #     advise.cant_up = True if direction == AdviseX.Up else False
        #     advise.cant_down = True if direction == AdviseX.Down else False
        #     advise.reason = f"收盘价小幅变动，同时成交量稍微上涨有限，并与MA05的趋势相反，说明市场没有上涨/下跌能量"
        #     return advise
        #
        # if 0 <= abs(rise) < 0.0003 and vol_prev > vol_now and times < 1:
        #     advise.direction = AdviseX.Up if self.ma05_trend.degree > 0 else AdviseX.Down if self.ma05_trend.degree < 0 else AdviseX.Unknown
        #     advise.degree = 10
        #     advise.cant_up = True if self.turn_direction(advise.direction) == AdviseX.Up else False
        #     advise.cant_down = True if self.turn_direction(advise.direction) == AdviseX.Down else False
        #     advise.reason = f"收盘价极小幅变动，如十字星，同时成交量变小，此时方向顺应趋势，反方向无市场能量"
        #     return advise
        #
        # if ((rise > 0 > rise_prev) or (rise < 0 < rise_prev)) and (abs(rise) < abs(rise_prev)) and \
        #         vol_now < vol_prev:
        #     advise.direction = AdviseX.Up if self.ma05_trend.degree > 0 else AdviseX.Down if self.ma05_trend.degree < 0 else AdviseX.Unknown
        #     advise.degree = 10
        #     advise.cant_up = True if self.turn_direction(advise.direction) == AdviseX.Up else False
        #     advise.cant_down = True if self.turn_direction(advise.direction) == AdviseX.Down else False
        #     advise.reason = f"幅度变动比上次小，方向相反，成交量比上次小，此时反弹完毕，反方向无市场能量"
        #     return advise
        #
        # # 收盘价小幅下跌，同时成交量下跌，而且成交量不小于1.5，说明市场没有下跌能量
        # if 0 < abs(rise) < 0.0005 and vol_now < vol_prev and (times > 1):
        #     advise.direction = self.turn_direction(direction)
        #     advise.degree = 10
        #     advise.cant_up = True if direction == AdviseX.Up else False
        #     advise.cant_down = True if direction == AdviseX.Down else False
        #     advise.reason = f"收盘价小幅上涨，同时成交量下跌，而且成交量不小于1，说明市场没有上涨能量"
        #     return advise
        #
        # # 两次连续，涨或者跌
        # advise.rise_running = 0
        # if running_close_count >= 2 and ((data.open[-1] > data.close[-1] and data.open[-2] > data.close[-2]) or \
        #                                  data.open[-1] < data.close[-1] and data.open[-2] < data.close[-2]):
        #     advise.rise_running = data.close[-(running_close_count + 1):].diff().dropna().sum() / data.close[-1]
        #     print(f"{self.running_time} 连续涨跌幅:{advise.rise_running}")
        #
        # if running_volume_count >= 2 and (round(abs(rise), 4) <= 0.0006) and vol_prev > vol_now:
        #     advise.direction = self.turn_direction(direction)
        #     advise.degree = 10
        #     advise.cant_up = True if direction == AdviseX.Up else False
        #     advise.cant_down = True if direction == AdviseX.Down else False
        #     advise.reason = f"连续{running_volume_count}次以上成交量收缩，且极小幅度({round(abs(rise), 4) * 100:.2f}%)变动，变向"
        #     return advise
        #
        # if running_volume_count >= 1 and running_close_count >= 2 and times > 1 and abs(over_points) > 12 and \
        #         ((vol_now > vol_prev and rise > rise_prev) or (vol_now < vol_prev and rise < rise_prev)):
        #     advise.direction = direction
        #     advise.degree = 10
        #     advise.cant_up = True if direction == AdviseX.Down else False
        #     advise.cant_down = True if direction == AdviseX.Up else False
        #     advise.reason = f"连续{running_volume_count}次以上成交量和收盘价放大，同向"
        #     return advise

        # return advise
        #
        # # 量大 幅度大 同向
        # # 量大 幅度不大 反向
        # if times >= 5:
        #     advise.direction = direction
        #     advise.degree = 20
        #     advise.reason = f"成交量(平均值5x以上)巨大，走势不变"
        #     return advise
        #
        # if times >= 3 and abs(rise) > 0.0015:
        #     advise.direction = direction
        #     advise.degree = 10
        #     advise.reason = f"成交量(平均值3x以上)大，且有大幅度({abs(rise) * 100:.2f}%)变动，走势不变"
        #     return advise
        #
        # if times > 1.5 and abs(rise) > 0.0015:
        #     advise.direction = direction
        #     advise.degree = 5
        #     advise.reason = f"成交量(平均值1.5x以上)大，且有大幅度({abs(rise) * 100:.2f}%)变动，走势不变"
        #     return advise
        #
        # if times > 1.5 and (abs(rise) < 0.0005):
        #     advise.direction = self.turn_direction(direction)
        #     advise.degree = 10
        #     advise.reason = f"成交量(平均值1.5x以上)大，且极小幅度({abs(rise) * 100:.2f}%)变动，变向"
        #     return advise
        #
        # return advise

    @staticmethod
    def get_running(bar, series, what):
        direction = Common.get_last_direction(bar, series)
        if direction == AdviseX.Up:
            for i in range(4):
                start = -6 + i
                res = (series[start:].diff().dropna() > 0).all()
                if res and start <= -3:
                    return VolPrice.sum_running(bar, series, direction, start, what)
        elif direction == AdviseX.Down:
            for i in range(4):
                start = -6 + i
                res = (series[start:].diff().dropna() < 0).all()
                if res and start <= -3:
                    return VolPrice.sum_running(bar, series, direction, start, what)

        return direction, 1, 0, 0

    @staticmethod
    def sum_running(bar, series, direction, start, what):
        running_count = - start - 1
        running_points = series[-(running_count + 1):].diff().dropna().sum()
        if what == "成交量":
            running_rise = round(running_points / series[-(running_count + 1)], 4)
        else:
            running_rise = round(running_points / series[-1], 4)
        # print(f"{bar.running_time} {what}连续{running_count}次{AdviseX.Weak_Up}")
        return direction, running_count, running_points, running_rise

    #
